package yxy.game.pm2.command.union.contrib;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.message.UnionMessage.ContributeType;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionContribution;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.player.UnionPlayer;
import yxy.game.pm2.module.union.player.UnionPlayerContribution;

/** 联盟捐献 联盟捐献界面 */
@MessageType(proto = Command.UnionContributeMain, classtype = UnionMessage.UnionContributeMainRequest.class)
public class CMD_UnionContributeMain extends Command<UnionMessage.UnionContributeMainRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionMessage.UnionContributeMainRequest request) {
		String playerCode = messageSource.getTag();

		UnionMessage.UnionContributeMainResponse.Builder response = UnionMessage.UnionContributeMainResponse.newBuilder();

		// 检查是否已经有联盟
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionIn)) {
			send(messageSource, response, result);
			return;
		}
		UnionBean union = entry.getUnionBean();
		UnionContribution contribution = UnionContribution.get(union);
		UnionPlayer unionPlayer = entry.getUnionPlayer();
		UnionPlayerContribution playerContribution = unionPlayer.getContribution();

		response.setDone(ContributeType.forNumber(playerContribution.getContribType().getNumber()));
		response.setProgress(contribution.getProgress());
		for (String boxId : playerContribution.getBox()) {
			response.addBox(Integer.valueOf(boxId));
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, UnionMessage.UnionContributeMainResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}